package com.ctshk.rpc.stamp.service.impl;

import com.alibaba.fastjson.JSON;
import com.ctshk.common.dto.Result;
import com.ctshk.common.enums.*;
import com.ctshk.common.exception.BusinessException;
import com.ctshk.common.utils.ValidatorUtil;
import com.ctshk.rpc.booking.enums.SupplierReserveTypeEnum;
import com.ctshk.rpc.booking.req.system.stampduty.add.SupplierOrderSystemStampDutyAddReq;
import com.ctshk.rpc.booking.req.system.stampduty.add.SupplierSystemOrderStampDutyAddReq;
import com.ctshk.rpc.booking.service.ISupplierOrderService;
import com.ctshk.rpc.stamp.dto.LevyDTO;
import com.ctshk.rpc.stamp.req.LevyRequest;
import com.ctshk.rpc.stamp.service.ILevyService;
import com.ctshk.rpc.stamp.thirdparty.tic.request.TicHttpsClient;
import com.ctshk.rpc.stamp.thirdparty.tic.schema.elevy.LevyEFrankRequest;
import com.ctshk.rpc.stamp.thirdparty.tic.service.elevy.IELevyService;
import com.ctshk.rpc.system.dto.UserDTO;
import com.ctshk.rpc.system.service.ISysUserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Value;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 缴纳印花税 服务实现类
 * </p>
 *
 * @author xietao
 * @since 2021-03-12
 */
@Slf4j
@DubboService
public class LevyServiceImpl implements ILevyService {

    @DubboReference
    private IELevyService levyService;

    @DubboReference
    private ISupplierOrderService supplierOrderService;

    @DubboReference
    private ISysUserService sysUserService;

    @Value("${tic.api-key}")
    private String API_KEY;

    /**
     * 印花税的RECEIPT_NUMBER和DEPARTURE_DATE是组合唯一
     *
     * @param request
     * @return
     */
    @Override
    public Result levyEFrank(LevyRequest request) {
        try {
            ValidatorUtil.validate(request);
            //组织入参
            LevyEFrankRequest levyEFrankRequest = new LevyEFrankRequest();
            levyEFrankRequest.setAPIKEY(API_KEY);
            levyEFrankRequest.setDEPARTUREDATE(request.getDepartureDate());
            levyEFrankRequest.setNAMEOFTRAVELERPAYER(request.getNameOfTravelerPayer());
            levyEFrankRequest.setRECEIPTNUMBER(request.getReceiptNumber());
            levyEFrankRequest.setLAST4DIGITOFCONTACTPHONENUMBER(request.getLast4DigitOfContactPhoneNumber());
            levyEFrankRequest.setOUTBOUNDFAREPAID(request.getPremiumReceived());
            log.info("【印花税-缴纳印花税】调用第三方印花税请求:{}", JSON.toJSONString(levyEFrankRequest));
            Result<LevyDTO> levyDTOResult = TicHttpsClient.levyEFrank(levyEFrankRequest);
            log.info("【印花税-缴纳印花税】调用第三方印花税响应:{}", JSON.toJSONString(levyDTOResult));
            if (!levyDTOResult.isSuccess()) {
                return levyDTOResult;
            }
            /**
             * 生成供应商预订
             */
            UserDTO userDTO = sysUserService.queryCacheUser(request.getUserId());
            SupplierOrderSystemStampDutyAddReq supplierOrderSystemStampDutyAddReq = new SupplierOrderSystemStampDutyAddReq();
            supplierOrderSystemStampDutyAddReq.setBusinessType(request.getBusinessType());
            supplierOrderSystemStampDutyAddReq.setProductType(ProductType.API_PRODUCT.getCode());
            supplierOrderSystemStampDutyAddReq.setSupplierId(62201617578983424L); // 写死中国太平保险
            supplierOrderSystemStampDutyAddReq.setOrderType(1);
            supplierOrderSystemStampDutyAddReq.setSupplierOrderType("1");
            supplierOrderSystemStampDutyAddReq.setProductName("印花税");
            supplierOrderSystemStampDutyAddReq.setSaleType(SalesMethod.SINGLE_SALE.getCode());
            supplierOrderSystemStampDutyAddReq.setReserveTypeId(SupplierReserveTypeEnum.STAMP_DUTY.getId());
            supplierOrderSystemStampDutyAddReq.setConfirmStatus(SupplierConfirmStatus.CONFIRMED.getCode());
            supplierOrderSystemStampDutyAddReq.setOriginalSaleNumber(userDTO.getDept());
            supplierOrderSystemStampDutyAddReq.setDepartmentId(userDTO.getDeptId());
            supplierOrderSystemStampDutyAddReq.setDepartment(userDTO.getDept());
            supplierOrderSystemStampDutyAddReq.setTotalFee(new BigDecimal(request.getPremiumReceived()));//业务订单金额
            supplierOrderSystemStampDutyAddReq.setPayableAmount(new BigDecimal(request.getPremiumReceived()));//业务订单金额
             supplierOrderSystemStampDutyAddReq.setSaleNumber(request.getReceiptNumber()); // 取订单号
            supplierOrderSystemStampDutyAddReq.setProductNumber(request.getReceiptNumber()); // 取收据单号
            supplierOrderSystemStampDutyAddReq.setCurrencyId(Currency.HKD.getCode()); //业务订单币种
            supplierOrderSystemStampDutyAddReq.setCurrency(Currency.HKD.getCurrency());
            // 供应商预订-保险信息
            List<SupplierSystemOrderStampDutyAddReq> supplierSystemOrderStampDutyAddReqList = new ArrayList<>();
            supplierOrderSystemStampDutyAddReq.setSupplierSystemOrderStampDutyAddReqList(supplierSystemOrderStampDutyAddReqList);
            SupplierSystemOrderStampDutyAddReq supplierSystemOrderStampDutyAddReq = new SupplierSystemOrderStampDutyAddReq();
            supplierSystemOrderStampDutyAddReqList.add(supplierSystemOrderStampDutyAddReq);

            supplierSystemOrderStampDutyAddReq.setMoney(new BigDecimal(request.getPremiumReceived()));
            supplierSystemOrderStampDutyAddReq.setReceiptNumber(request.getReceiptNumber());
            log.info("【印花税-缴纳印花税】调用供应商预订请求,req:{},userId:{}", JSON.toJSONString(supplierOrderSystemStampDutyAddReq));
            Result<String> supplierResult = supplierOrderService.addSystemStampDuty(supplierOrderSystemStampDutyAddReq, userDTO.getId());
            if(!supplierResult.isSuccess()){
                throw new BusinessException(SystemError.getDefined(supplierResult.getCode()));
            }
            levyDTOResult.getData().setSupplierOrderNumber(supplierResult.getData());
            return levyDTOResult;
        } catch (BusinessException e) {
            log.error("【印花税-缴纳印花税】缴纳印花税业务异常,error:{}", e.getMessage(), e);
            return Result.failed(SystemError.getDefined(e.getCode()));
        } catch (Exception e) {
            log.error("【印花税-缴纳印花税】缴纳印花税异常,error:{}", e.getMessage(), e);
            return Result.failed(SystemError.SYS_500);
        }
    }
}
